import Cookies from 'js-cookie'

export default {
  // 设置永久缓存
  setLocal(key: string, val: any) {
    window.localStorage.setItem(key, JSON.stringify(val))
  },

  //  获取永久缓存
  getLocal(key: string) {
    const json: any = window.localStorage.getItem(key)
    return JSON.parse(json)
  },

  // 移除永久缓存
  removeLocal(key: string) {
    window.localStorage.removeItem(key)
  },

  // 移除全部永久缓存
  clearLocal() {
    window.localStorage.clear()
  },

  // 设置临时缓存
  setSession(key: string, val: any) {
    if (key === 'key') return Cookies.set(key, val)
    window.sessionStorage.setItem(key, JSON.stringify(val))
  },

  // 获取临时缓存
  getSession(key: string) {
    if (key === 'key') return Cookies.get(key)
    const json: any = window.sessionStorage.getItem(key)
    if (json && json !== 'undefined') {
      return JSON.parse(json)
    }
  },

  // 移除临时缓存
  removeSession(key: string) {
    if (key === 'key') return Cookies.remove(key)
    window.sessionStorage.removeItem(key)
  },

  // 移除全部临时缓存
  clearSession() {
    Cookies.remove('key')
    window.sessionStorage.clear()
  }
}
